<?php
/**
 * Created by PhpStorm.
 * User: Aliaxandr Lazerka
 * Date: 10/26/14
 * Time: 10:19 AM
 */
include_once("functions/db_manipulate.php");
include_once("Security.php");

$encPass = NULL;

if ($_POST['usrName'] != null AND $_POST['pass'] != null) {
  $encPass = Security::encryptPassword($_POST['usrName'], $_POST['pass']);

  $array = getUsrIdRoleByPassAndUsrName($encPass,$_POST['usrName']);

  if ($array != null) {
    session_start();
    $_SESSION['USR_ID'] = $array['ID'];
    $_SESSION['USR_ROLE_ID'] = $array['ROLE_ID'];
    header("Location:?main");
  } else {
    header("Location: " . $_POST['url'] . "&err=1021");
  }
} else {
  if ($_POST['usrName'] == null AND $_POST['pass'] == null) {
    header("Location: " . $_POST['url'] . "&err=100");
  } elseif ($_POST['usrName'] == null) {
    header("Location: " . $_POST['url'] . "&err=1010");
  } elseif ($_POST['pass'] == null) {
    header("Location: " . $_POST['url'] . "&err=1020");
  }
}